<?php

$config_db		= [];
$config_insert	= '';

// needed to allow config variables to be accessed within some functions like _quote or insert_pages.
global $config_global, $dblink_global;
$config_global	= $config;
$dblink_global	= $dblink;

// inserting secondary config values
$config_db['abuse_email']					= $config['admin_email'];
$config_db['account_page']					= $config['account_page'];
$config_db['acl_lock']						= $config['acl_lock'];
$config_db['admin_email']					= $config['admin_email'];
$config_db['admin_name']					= $config['admin_name'];
$config_db['allow_email_reuse']				= $config['allow_email_reuse'];
$config_db['allow_intercom']				= $config['allow_intercom'];
$config_db['allow_license_per_page']		= $config['allow_license_per_page'];
$config_db['allow_massemail']				= $config['allow_massemail'];
$config_db['allow_persistent_cookie']		= $config['allow_persistent_cookie'];
$config_db['allow_rawhtml']					= $config['allow_rawhtml'];
$config_db['allow_registration']			= $config['allow_registration'];
$config_db['allow_themes']					= $config['allow_themes'];
$config_db['allow_themes_per_page']			= $config['allow_themes_per_page'];
$config_db['allow_x11colors']				= $config['allow_x11colors'];
$config_db['allowed_email_domains']			= $config['allowed_email_domains'];
$config_db['allowed_languages']				= $config['allowed_languages'];
$config_db['american_date']					= $config['american_date'];
$config_db['anonymize_ip']					= $config['anonymize_ip'];
$config_db['ap_failed_login_count']			= $config['ap_failed_login_count'];
$config_db['ap_max_login_attempts']			= $config['ap_max_login_attempts'];
$config_db['approve_new_user']				= $config['approve_new_user'];
$config_db['attachments_handler']			= $config['attachments_handler'];
$config_db['autosubscribe']					= $config['autosubscribe'];
$config_db['cache']							= $config['cache'];
$config_db['cache_sql']						= $config['cache_sql'];
$config_db['cache_sql_ttl']					= $config['cache_sql_ttl'];
$config_db['cache_ttl']						= $config['cache_ttl'];
$config_db['canonical']						= $config['canonical'];
$config_db['captcha_edit_page']				= $config['captcha_edit_page'];
$config_db['captcha_new_comment']			= $config['captcha_new_comment'];
$config_db['captcha_new_page']				= $config['captcha_new_page'];
$config_db['captcha_registration']			= $config['captcha_registration'];
$config_db['categories_handler']			= $config['categories_handler'];
$config_db['category_page']					= $config['category_page'];
$config_db['changes_page']					= $config['changes_page'];
$config_db['check_mimetype']				= $config['check_mimetype'];
$config_db['comments_count']				= $config['comments_count'];
$config_db['comment_delay']					= $config['comment_delay'];
$config_db['comments_offset']				= $config['comments_offset'];
$config_db['comments_page']					= $config['comments_page'];
$config_db['create_thumbnail']				= $config['create_thumbnail'];
$config_db['csp']							= $config['csp'];
$config_db['date_format']					= $config['date_format'];
$config_db['debug']							= $config['debug'];
$config_db['debug_admin_only']				= $config['debug_admin_only'];
$config_db['debug_sql_threshold']			= $config['debug_sql_threshold'];
$config_db['default_comment_acl']			= $config['default_comment_acl'];
$config_db['default_create_acl']			= $config['default_create_acl'];
$config_db['default_diff_mode']				= $config['default_diff_mode'];
$config_db['default_read_acl']				= $config['default_read_acl'];
$config_db['default_rename_redirect']		= $config['default_rename_redirect'];
$config_db['default_upload_acl']			= $config['default_upload_acl'];
$config_db['default_write_acl']				= $config['default_write_acl'];
$config_db['diff_modes']					= $config['diff_modes'];
$config_db['disable_bracketslinks']			= $config['disable_bracketslinks'];
$config_db['disable_formatters']			= $config['disable_formatters'];
$config_db['disable_safehtml']				= $config['disable_safehtml'];
$config_db['disable_wikilinks']				= $config['disable_wikilinks'];
$config_db['disable_wikiname']				= $config['disable_wikiname'];
$config_db['edit_summary']					= $config['edit_summary'];
$config_db['email_confirmation']			= $config['email_confirmation'];
$config_db['email_from']					= $config['email_from'];
$config_db['email_priority']				= $config['email_priority'];
$config_db['email_subject_prefix']			= $config['email_subject_prefix'];
$config_db['enable_captcha']				= $config['enable_captcha'];
$config_db['enable_comments']				= $config['enable_comments'];
$config_db['enable_counters']				= $config['enable_counters'];
$config_db['enable_email']					= $config['enable_email'];
$config_db['enable_email_notification']		= $config['enable_email_notification'];
$config_db['enable_feeds']					= $config['enable_feeds'];
$config_db['enable_license']				= $config['enable_license'];
$config_db['enable_referrers']				= $config['enable_referrers'];
$config_db['enable_security_headers']		= $config['enable_security_headers'];
$config_db['enable_system_message']			= $config['enable_system_message'];
$config_db['export_handler']				= $config['export_handler'];
$config_db['ext_bad_behaviour']				= $config['ext_bad_behaviour'];
$config_db['footer_comments']				= $config['footer_comments'];
$config_db['footer_files']					= $config['footer_files'];
$config_db['footer_tags']					= $config['footer_tags'];
$config_db['forbidden_email_domains']		= $config['forbidden_email_domains'];
$config_db['form_token_time']				= $config['form_token_time'];
$config_db['forum_cluster']					= $config['forum_cluster'];
$config_db['forum_topics']					= $config['forum_topics'];
$config_db['groups_page']					= $config['groups_page'];
$config_db['help_page']						= $config['help_page'];
$config_db['hide_index']					= $config['hide_index'];
$config_db['hide_locked']					= $config['hide_locked'];
$config_db['hide_revisions']				= $config['hide_revisions'];
$config_db['hide_toc']						= $config['hide_toc'];
$config_db['index_page']					= $config['index_page'];
$config_db['intercom_delay']				= $config['intercom_delay'];
$config_db['ip_login_limit_max']			= $config['ip_login_limit_max'];
$config_db['jpeg_quality']					= $config['jpeg_quality'];
$config_db['keep_deleted_time']				= $config['keep_deleted_time'];
$config_db['language']						= $config['language'];
$config_db['license']						= $config['license'];
$config_db['link_target']					= $config['link_target'];
$config_db['list_count']					= $config['list_count'];
$config_db['log_default_show']				= $config['log_default_show'];
$config_db['log_level']						= $config['log_level'];
$config_db['log_purge_time']				= $config['log_purge_time'];
$config_db['login_notice']					= $config['login_notice'];
$config_db['login_page']					= $config['login_page'];
$config_db['site_logo']						= $config['site_logo'];
$config_db['logo_display']					= $config['logo_display'];
$config_db['logo_height']					= $config['logo_height'];
$config_db['logo_width']					= $config['logo_width'];
$config_db['maint_last_cache']				= 1;
$config_db['maint_last_delpages']			= 1;
$config_db['maint_last_log']				= 1;
$config_db['maint_last_oldpages']			= 1;
$config_db['maint_last_refs']				= 1;
$config_db['maint_last_session']			= 1;
$config_db['maint_last_xml_sitemap']		= 1;
$config_db['max_image_area']				= $config['max_image_area'];
$config_db['max_image_width']				= $config['max_image_width'];
$config_db['max_login_attempts']			= $config['max_login_attempts'];
$config_db['max_page_size']					= $config['max_page_size'];
$config_db['max_thumb_width']				= $config['max_thumb_width'];
$config_db['menu_items']					= $config['menu_items'];
$config_db['minor_edit']					= $config['minor_edit'];
$config_db['multilanguage']					= $config['multilanguage'];
$config_db['name_date_macro']				= $config['name_date_macro'];
$config_db['news_cluster']					= $config['news_cluster'];
$config_db['news_levels']					= $config['news_levels'];
$config_db['news_structure']				= $config['news_structure'];
$config_db['nofollow']						= $config['nofollow'];
$config_db['noindex']						= $config['noindex'];
$config_db['noreferrer']					= $config['noreferrer'];
$config_db['noreply_email']					= $config['noreply_email'];
$config_db['notify_comment']				= $config['notify_comment'];
$config_db['notify_diff_mode']				= $config['notify_diff_mode'];
$config_db['notify_minor_edit']				= $config['notify_minor_edit'];
$config_db['notify_new_user_account']		= $config['notify_new_user_account'];
$config_db['notify_page']					= $config['notify_page'];
$config_db['notify_upload']					= $config['notify_upload'];
$config_db['numerate_links']				= $config['numerate_links'];
$config_db['opensearch']					= $config['opensearch'];
$config_db['owners_can_remove_comments']	= $config['owners_can_remove_comments'];
$config_db['pages_purge_time']				= $config['pages_purge_time'];
$config_db['paragrafica']					= $config['paragrafica'];
$config_db['password_page']					= $config['password_page'];
$config_db['permissions_policy']			= $config['permissions_policy'];
$config_db['phpmailer_method']				= $config['phpmailer_method'];
$config_db['privacy_page']					= $config['privacy_page'];
$config_db['publish_anonymously']			= $config['publish_anonymously'];
$config_db['pwd_admin_min_chars']			= $config['pwd_admin_min_chars'];
$config_db['pwd_char_classes']				= $config['pwd_char_classes'];
$config_db['pwd_min_chars']					= $config['pwd_min_chars'];
$config_db['pwd_unlike_login']				= $config['pwd_unlike_login'];
$config_db['random_page']					= $config['random_page'];
$config_db['referrer_policy']				= $config['referrer_policy'];
$config_db['referrers_purge_time']			= $config['referrers_purge_time'];
$config_db['registration_delay']			= $config['registration_delay'];
$config_db['registration_page']				= $config['registration_page'];
$config_db['remove_onlyadmins']				= $config['remove_onlyadmins'];
$config_db['rename_global_acl']				= $config['rename_global_acl'];
$config_db['reserved_namespaces']			= $config['reserved_namespaces'];
$config_db['reverse_proxy']					= $config['reverse_proxy'];
$config_db['reverse_proxy_header']			= $config['reverse_proxy_header'];
$config_db['reverse_proxy_addresses']		= $config['reverse_proxy_addresses'];
$config_db['review']						= $config['review'];
$config_db['rewrite_mode']					= $config['rewrite_mode'];
$config_db['root_page']						= $config['root_page'];
$config_db['search_page']					= $config['search_page'];
$config_db['section_edit']					= $config['section_edit'];
$config_db['session_length']				= $config['session_length'];
$config_db['session_notice']				= $config['session_notice'];
$config_db['session_store']					= $config['session_store'];
$config_db['show_permalink']				= $config['show_permalink'];
$config_db['show_spaces']					= $config['show_spaces'];
$config_db['site_desc']						= $config['site_desc'];
$config_db['site_favicon']					= $config['site_favicon'];
$config_db['site_name']						= $config['site_name'];
$config_db['source_handler']				= $config['source_handler'];
$config_db['smtp_connection_mode']			= $config['smtp_connection_mode'];
$config_db['smtp_host']						= $config['smtp_host'];
$config_db['smtp_password']					= $config['smtp_password'];
$config_db['smtp_port']						= $config['smtp_port'];
$config_db['smtp_username']					= $config['smtp_username'];
$config_db['sorting_comments']				= $config['sorting_comments'];
$config_db['spam_filter']					= $config['spam_filter'];
$config_db['standard_handlers']				= $config['standard_handlers'];
$config_db['store_deleted_pages']			= $config['store_deleted_pages'];
$config_db['svg_sanitizer']					= $config['svg_sanitizer'];
$config_db['system_message']				= $config['system_message'];
$config_db['system_message_audience']		= $config['system_message_audience'];
$config_db['system_message_type']			= $config['system_message_type'];
$config_db['terms_page']					= $config['terms_page'];
$config_db['theme']							= $config['theme'];
$config_db['theme_color']					= $config['theme_color'];
$config_db['time_format']					= $config['time_format'];
$config_db['time_format_seconds']			= $config['time_format_seconds'];
$config_db['timezone']						= $config['timezone'];
$config_db['tls']							= $config['tls'];
$config_db['tls_implicit']					= $config['tls_implicit'];
$config_db['tree_level']					= $config['tree_level'];
$config_db['typografica']					= $config['typografica'];
$config_db['upload']						= $config['upload'];
$config_db['upload_allowed_exts']			= $config['upload_allowed_exts'];
$config_db['upload_banned_exts']			= $config['upload_banned_exts'];
$config_db['upload_images_only']			= $config['upload_images_only'];
$config_db['upload_quota']					= $config['upload_quota'];
$config_db['upload_quota_per_user']			= $config['upload_quota_per_user'];
$config_db['upload_max_size']				= $config['upload_max_size'];
$config_db['upload_translit']				= $config['upload_translit'];
$config_db['upload_translit_lower']			= $config['upload_translit_lower'];
$config_db['urls_underscores']				= $config['urls_underscores'];
$config_db['username_chars_min']			= $config['username_chars_min'];
$config_db['username_chars_max']			= $config['username_chars_max'];
$config_db['user_can_delete_account']		= $config['user_can_delete_account'];
$config_db['users_page']					= $config['users_page'];
$config_db['whatsnew_page']					= $config['whatsnew_page'];
$config_db['xml_changes_link']				= $config['xml_changes_link'];
$config_db['xml_sitemap']					= $config['xml_sitemap'];
$config_db['xml_sitemap_gz']				= $config['xml_sitemap_gz'];
$config_db['xml_sitemap_time']				= $config['xml_sitemap_time'];
$config_db['xml_sitemap_update']			= null;
$config_db['youarehere_text']				= $config['youarehere_text'];

foreach ($config_db as $key => $value)
{
	$config_insert .= "('$key', '" . _q($value) . "'), ";
}

$config_insert .= "('maint_last_update', " . utc_dt() . ") ";

// to update existing values we use INSERT ... ON DUPLICATE KEY UPDATE
// https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
$insert_config =	"INSERT INTO " . $config['table_prefix'] . "config (config_name, config_value)
						VALUES " .
							$config_insert .
						(in_array($config_global['db_driver'], ['sqlite', 'sqlite_pdo'])
							? "ON CONFLICT(config_name) DO UPDATE SET
								config_name		= excluded.config_name,
								config_value	= excluded.config_value;"
							: "ON DUPLICATE KEY UPDATE
								config_name		= VALUES(config_name),
								config_value	= VALUES(config_value);"
						); // FIXME: ON CONFLICT(config_id, config_name)
